package com.itouchtv.yang.library.page;

import cn.hutool.core.text.csv.CsvRow;
import com.itouchtv.yang.core.driver.BaseDriver;
import com.itouchtv.yang.core.driver.CommDriver;
import com.itouchtv.yang.core.tools.Log;
import com.itouchtv.yang.core.tools.UpLoadPic;
import com.itouchtv.yang.library.element.ElementBy;
import com.itouchtv.yang.library.element.PageElement;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * page包装基类
 */
public class PageObject {
    protected BaseDriver driver;
    private static Map<String ,By> bys;

    public PageObject(){
        this.driver = CommDriver.driver.get();
        if (Objects.isNull(bys)){
            bys = ElementBy.getElementBy();
        }
    }

    PageElement findElement(String elementName) {
        By by = bys.get(elementName);
        Log.logger.info("定位元素："+elementName);
        return driver.findElement(by);
    }

    List<WebElement> findElements(By by) {
        return driver.findElements(by);
    }

    @Given("^使用([\\w\\W]*)打开页面:([a-zA-z]+://[^\\s]*)$")
    public void open(String device,String url){
        driver = new BaseDriver(device);
        CommDriver.driver.set(driver);
        driver.openUrl(url);
    }

    public void go(String url){
        driver.openUrl(url);
    }

    @When("^点击:([\\w\\W]*)$")
    public void clickElement(String elementName) {
        Log.logger.info("点击按钮："+elementName);
        driver.onClick(findElement(elementName));

    }

    @When("^鼠标点击:([\\w\\W]*)$")
    public void mouseClickElement(String elementName){
        Log.logger.info("鼠标点击:"+elementName);
        driver.mouseClick(findElement(elementName));
    }

    public void inputText(String elementName, String text, Boolean clear) {
        Log.logger.info("在"+elementName+"内输入："+text);
        driver.sendKey(findElement(elementName), text, clear);
    }

    @When("^在([\\w\\W]*)内输入:([\\w\\W]*)$")
    public void inputText(String elementName, String text) {
        Log.logger.info("在"+elementName+"内输入："+text);
        driver.sendKey(findElement(elementName), text, true);
    }

    @When("^上传文件:([\\w\\W]*)$")
    public void alertUpload(String filePath){
        UpLoadPic.upload(filePath);
    }

    public void clickAlertAccept(){
        driver.clickAlertAccept();
    }

    public void clickAlertDismiss(){
        driver.clickAlertDismiss();
    }

    public void sendAndClickAlert(String keys){
        driver.sendAndClickAlert(keys);
    }

    public void back(){
        driver.back();
    }

    @Then("^检查元素([\\w\\W]*)的文本:([\\w\\W]*)$")
    public void checkElenentText(String elementName, String message) {
        driver.checkText(findElement(elementName), message);
    }

    public void checkAttribute(String elementName, String attributeName, String value) {
        driver.checkAttribute(findElement(elementName), attributeName, value);
    }

    public void checkAlertText(String text){
        driver.checkAlertText(text);
    }

    public void checkIsDisplayed(String elementName,Boolean displayed) {
        driver.checkDisplayed(findElement(elementName),displayed);
    }

    public void checkisEnabled(String elementName,Boolean enabled) {
        driver.checkEnabled(findElement(elementName),enabled);
    }

    public void checkIsSelected(String elementName,Boolean selected) {
        driver.checkSelected(findElement(elementName),selected);
    }

    public void checkPageContainsText(String text) {
        driver.checkPageString(text);
    }

    @Then("^存在元素:([\\w\\W]*)$")
    public void checkExistWebElement(String elementName) {
        driver.checkExistWebElement(findElement(elementName));
    }

    public void checkUrl(String url) {
        driver.checkUrl(url);
    }

    public void checkTitle(String title) {
        driver.checkTitle(title);
    }

    public void switchtoNewWindow(int index){
        driver.switchtoWindow(index);
    }

    public void closeWindow(){
        driver.closeWindow();
    }

    public void closeBrowser(){
        driver.closeBrowser();
    }

    public void sleep(long time){
        driver.sleep(time);
    }

    public void action(CsvRow csvRow) {
        String action = csvRow.getByName("操作");
        String element = csvRow.getByName("元素");
        String data = csvRow.getByName("数据");
        String device = csvRow.getByName("browser");
        switch (action) {
            case "打开":
                open(device,data);
                break;
            case "输入":
                inputText(element, data, true);
                break;
            case "点击":
                clickElement(element);
                break;
            case "鼠标点击":
                mouseClickElement(element);
                break;
            case "上传":
                alertUpload(data);
                break;
            case "弹出提示确定":
                clickAlertAccept();
                break;
            case "弹出提示取消":
                clickAlertDismiss();
                break;
            case "弹出提示输入并确定":
                Objects.requireNonNull(data, "没有填写输入内容");
                sendAndClickAlert(data);
                break;
            case "点击返回":
                back();
                break;
            case "检查元素文本":
                checkElenentText(element, data);
                break;
            case "检查元素属性":
                String[] s = data.split(":");
                Assert.assertEquals(s.length, 2);
                checkAttribute(element, s[0], s[1]);
                break;
            case "检查弹出提示文本":
                checkAlertText(data);
                break;
            case "检查元素是否可见":
                Boolean isDisplayed;
                if (Objects.equals(data, "true")) {
                    isDisplayed = true;
                } else isDisplayed = false;
                checkIsDisplayed(element, isDisplayed);
                break;
            case "检查元素是否激活":
                Boolean isElabled;
                if (Objects.equals(data, "true")) {
                    isElabled = true;
                } else isElabled = false;
                checkisEnabled(element, isElabled);
                break;
            case "检查元素是否被选中":
                Boolean isSelected;
                if (Objects.equals(data, "true")) {
                    isSelected = true;
                } else isSelected = false;
                checkIsSelected(element, isSelected);
                break;
            case "检查页面文本":
                checkPageContainsText(data);
                break;
            case "检查元素是否存在":
                checkExistWebElement(element);
                break;
            case "检查URL":
                checkUrl(data);
                break;
            case "检查url":
                checkUrl(data);
                break;
            case "检查标题":
                checkTitle(data);
                break;
            case "切换窗口":
                switchtoNewWindow(Integer.parseInt(data));
                break;
            case "关闭窗口":
                closeWindow();
                break;
            case "关闭浏览器":
                closeBrowser();
                break;
            default:
                throw new NotFoundException("未发现关健字" + action + "请检查");
        }
    }
}
